/* Data analysis of Endline Data for Nepal CCF: Children */

local folder "C:\Users\mlevere\OneDrive - Mathematica\Documents\Projects\Nepal\"

/* Merge together all the sections into one big dataset 
   Rename all the variables along the way */
   
/* First, get the ID-codes of children under age 3 */
use "`folder'/Data/Endline_Raw/SEC10B", clear
rename q10_name child_name
rename q10_1007dd child_bday_day
rename q10_1007mm child_bday_month
rename q10_1007yy child_bday_year
rename q10_1008 child_complications
rename q10_1009 child_length_breastfed
rename q10_1010 child_occulated
rename q10_1011 child_height
rename q10_1012 child_height_laying
rename q10_1013 child_weight
rename q10_1014 child_weight_alone
rename q10_1015 child_noanthro_reasons
rename q10_idc q11_idc
tempfile endline_raw_child
save "`endline_raw_child'", replace
  
merge 1:1 www hh sn idc q11_idc using "`folder'/Data/Endline_Raw/SEC11", nogen
/* Clean up ASQ data to be 0/1, create scores of modules */
label define asq_label 0 "No" 1 "Yes"

forvalues j=1/9 {
replace q11_1101a0`j' = 0 if q11_1101a0`j' == 2
label values q11_1101a0`j' asq_label
replace q11_1101b0`j' = 0 if q11_1101b0`j' == 2
label values q11_1101b0`j' asq_label
replace q11_1101c0`j' = 0 if q11_1101c0`j' == 2
label values q11_1101c0`j' asq_label
replace q11_1101d0`j' = 0 if q11_1101d0`j' == 2
label values q11_1101d0`j' asq_label
replace q11_1101e0`j' = 0 if q11_1101e0`j' == 2
label values q11_1101e0`j' asq_label
}

forvalues j=10/42 {
replace q11_1101a`j' = 0 if q11_1101a`j' == 2
label values q11_1101a`j' asq_label
}

forvalues j=10/41 {
replace q11_1101b`j' = 0 if q11_1101b`j' == 2
label values q11_1101b`j' asq_label
}

forvalues j=10/40 {
replace q11_1101c`j' = 0 if q11_1101c`j' == 2
label values q11_1101c`j' asq_label
}

forvalues j=10/41 {
replace q11_1101d`j' = 0 if q11_1101d`j' == 2
label values q11_1101d`j' asq_label
}

forvalues j=10/43 {
replace q11_1101e`j' = 0 if q11_1101e`j' == 2
label values q11_1101e`j' asq_label
}

egen asq_communication = rowtotal(q11_1101a01-q11_1101a32)
egen asq_grossmotor = rowtotal(q11_1101b01-q11_1101b33)
egen asq_finemotor = rowtotal(q11_1101c01-q11_1101c29)
egen asq_problemsolving = rowtotal(q11_1101d01-q11_1101d32)
egen asq_personalsocial = rowtotal(q11_1101e01-q11_1101e34)

/* Kids older than 37 months did not have a correct version of ASQ, toss out their
   scores */
replace asq_communication = . if q11_age > 36
replace asq_grossmotor = . if q11_age > 36
replace asq_finemotor = . if q11_age > 36
replace asq_problemsolving = . if q11_age > 36
replace asq_personalsocial = . if q11_age > 36


label variable asq_communication "ASQ Composite Commuication Score"
label variable asq_grossmotor "ASQ Composite Gross Motor Score"
label variable asq_finemotor "ASQ Composite Fine Motor Score"
label variable asq_problemsolving "ASQ Composite Problem Solving Score"
label variable asq_personalsocial "ASQ Composite Personal Social Score"

label define asq_comp_label 7 "Oops"

label values asq_communication asq_comp_label
label values asq_grossmotor asq_comp_label
label values asq_finemotor asq_comp_label
label values asq_problemsolving asq_comp_label
label values asq_personalsocial asq_comp_label

/* Grab the women's data:
   210 are not matched (all missing kids info): these include 173 for women who did not answer the women's
   survey stuff, 19 who are pregnant for the first time, 2 have youngest died */  
merge m:1 www hh sn idc using "`folder'/Data/endline_raw_women", nogen keep(matches)
drop q00_*
save "`endline_raw_child'", replace

/* Get the identification of age etc from the roster */
rename q11_idc q03_idc
merge 1:1 www hh sn q03_idc using "`folder'/Data/Endline_Raw/SEC03", nogen keep(matches) keepusing(q03_303 q03_304a q03_304b)
rename q03_303 child_sex
rename q03_304a child_age_years
rename q03_304b child_age_months
gen child_tot_months = child_age_years*12 + child_age_months
save "`endline_raw_child'", replace

/* Add in the morbidity from section 6 */
use "`folder'/Data/Endline_Raw/sec06B", clear
by www hh q06_idc, sort: keep if _n == 1
tempfile sec06B_clean
save "`sec06B_clean'", replace

use "`endline_raw_child'", clear
rename q03_idc q06_idc
merge 1:1 www hh sn q06_idc using "`sec06B_clean'", nogen keep(1 3)
gen child_sick = q06_602 ~= ""
rename q06_603 child_illness_type
rename q06_604 child_illness_where
rename q06_605 child_illness_consult_whom
rename q06_606 child_illness_noconsult
drop q06_602
save "`endline_raw_child'", replace

rename q06_idc q00_idc

merge 1:1 www hh sn q00_idc using "`folder'/Data/Endline_Raw/SEC00", nogen keep(match)
rename q00_idc idc_child

save "`folder'/Data/endline_raw_child", replace
